﻿using B.S.BaseData.Domain.Entities;
using B.S.BaseData.ErrorCode;
using B.S.BaseData.Infrastructure.Interfaces;
using B.S.BaseData.Read.API.Command.RoleCommand;
using MediatR;

namespace B.S.BaseData.Read.API.CommandHandler.RoleHandler
{
    public class RoleFanTianCommandHandler : IRequestHandler<RoleFanTianCommand, APIResult<Role>>
    {

        private readonly IBaseRepository<Role> roleRepos;

        public RoleFanTianCommandHandler(IBaseRepository<Role> roleRepos)
        {
            this.roleRepos = roleRepos;
        }

        public Task<APIResult<Role>> Handle(RoleFanTianCommand request, CancellationToken cancellationToken)
        {
            APIResult<Role> result = new APIResult<Role>();
            var obj = roleRepos.GetAll().FirstOrDefault(x => x.RoleId == request.RoleId);
            if (obj == null)
            {
                result.Code = APIEnum.Failed;
                result.Msg = "查询失败";
            }
            else
            {
                result.Code = APIEnum.Success;
                result.Msg = "查询成功";
                result.Data = obj;
            }
            return Task.FromResult(result);
        }
    }
}
